package org.keqinglee.study.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @description: org.keqinglee.study.io
 * @author: liqing
 * @date: 2022/8/31
 */
public class InputStreamTest {

    public static void main(String[] args) throws Exception {
        testBuffered();
    }

    /**
     * 测试缓冲流，实现文件拷贝
     *
     * @throws Exception
     */
    public static void testBuffered() throws Exception {
        try (FileInputStream fis = new FileInputStream("D:\\tangtang.jpg");
             BufferedInputStream bis = new BufferedInputStream(fis);
             FileOutputStream fos = new FileOutputStream("D:\\tangtang2.jpg");
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {

            //4、将文件读入到内存然后写出到目的地址
            byte[] buffer = new byte[64];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            throw e;
        }
    }

}
